package cn.yunyichina.provider.reservation.component;

import cn.yunyichina.provider.reservation.constant.Redis;
import cn.yunyichina.provider.reservation.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Created by Lullaby on 2016/8/30.
 */
@Component
public class NoBuilder {

    private static final String ORDER_NO_SIMBOL = "Y";

    private static final String BOOK_NO_SIMBOL = "C";

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public String generateOrderNo() {
        StringBuffer buffer = new StringBuffer();
        String timestr = DateUtil.format(Calendar.getInstance().getTime(), DateUtil.YYYYMMDDHHMMSS_NOBAR);
        String val = stringRedisTemplate.opsForValue().get(Redis.ORDERNO_DAILY_SIMBOL.concat(DateUtil.getTodayString()));
        if (StringUtils.isBlank(val) || "nil".equals(val)) {
            stringRedisTemplate.opsForValue().set(Redis.ORDERNO_DAILY_SIMBOL.concat(DateUtil.getTodayString()), "1");
            return buffer.append(ORDER_NO_SIMBOL).append(timestr).append("001").toString();
        }
        int value = Integer.parseInt(val) + 1;
        if (value > 0 && value < 10) {
            buffer.append(ORDER_NO_SIMBOL).append(timestr).append("00").append(value);
        } else if (value >= 10 && value < 100) {
            buffer.append(ORDER_NO_SIMBOL).append(timestr).append("0").append(value);
        } else {
            buffer.append(ORDER_NO_SIMBOL).append(timestr).append(value);
        }
        stringRedisTemplate.opsForValue().set(Redis.ORDERNO_DAILY_SIMBOL.concat(DateUtil.getTodayString()), String.valueOf(value));
        return buffer.toString();
    }

    public String generateReserveNo() {
        StringBuffer buffer = new StringBuffer();
        String val = stringRedisTemplate.opsForValue().get(Redis.BOOKNO_DAILY_SIMBOL.concat(DateUtil.getTodayString()));
        if (StringUtils.isBlank(val) || "nil".equals(val)) {
            stringRedisTemplate.opsForValue().set(Redis.BOOKNO_DAILY_SIMBOL.concat(DateUtil.getTodayString()), "1");
            return buffer.append(BOOK_NO_SIMBOL).append("00001").toString();
        }
        int value = Integer.parseInt(val) + 1;
        if (value > 0 && value < 10) {
            buffer.append(BOOK_NO_SIMBOL).append("0000").append(value);
        } else if (value >= 10 && value < 100) {
            buffer.append(BOOK_NO_SIMBOL).append("000").append(value);
        } else if (value >= 100 && value < 1000) {
            buffer.append(BOOK_NO_SIMBOL).append("00").append(value);
        } else if (value >= 1000 && value < 10000) {
            buffer.append(BOOK_NO_SIMBOL).append("0").append(value);
        } else {
            buffer.append(BOOK_NO_SIMBOL).append(value);
        }
        stringRedisTemplate.opsForValue().set(Redis.BOOKNO_DAILY_SIMBOL.concat(DateUtil.getTodayString()), String.valueOf(value));
        return buffer.toString();
    }

}
